package com.gzgs.controller;


import com.gzgs.client.UserClient;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("consumer/user")
@DefaultProperties(defaultFallback ="fallbachMethod")
public class UserController {

    @Autowired
    private UserClient userClient;

    @GetMapping
    @ResponseBody
    @HystrixCommand//标记该方法需要熔断
    public String queryUserById(@RequestParam("id") Integer id){
        if(id==1) throw new RuntimeException("它太忙了");
        String user = this.userClient.queryById(id);
        return user;
    }

    public String fallbachMethod(){
        return "服务器正忙，请稍后再试！";
    }
}
